package com.hm.controller;

import com.hm.mapper.StudentMapper;
import com.hm.pojo.entity.Student;
import com.hm.pojo.vo.StudentVo;
import com.hm.service.StudentService;
import com.hm.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: qy163-springboot04
 * @description:
 * @author: 胡萌
 * @create: 2023-04-12 19:30
 **/
@RestController
@RequestMapping("student")
@Api(tags = "学生操作API")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Resource
    private StudentMapper studentMapper;

    StudentVo studentVo = new StudentVo();

    @PostMapping("list/{current}/{pageSize}")
    @ApiOperation(value = "根据条件分页查询学生信息")
    public Result list(
            @ApiParam(value = "当前页面", name = "current", required = true, defaultValue = "1")
            @PathVariable Integer current,
            @ApiParam(value = "当前页数", name = "pageSize", required = true, defaultValue = "5")
            @PathVariable Integer pageSize,
            @RequestBody StudentVo studentVo) {

        return studentService.findByPage(current, pageSize, studentVo);
    }

    //@PutMapping  查询 get提交  删除 delete  修改 PUT提交  添加 post提交

    /*查   根据id查询*/
    @GetMapping("findById/{sid}")
    @ApiOperation(value = "根据id查询学生信息")
    public Student findAById(
            @ApiParam(value = "学生id", name = "sid", required = true)
            @PathVariable Integer sid
    ){
        Student student = studentMapper.selectById(sid);
        return  student;
    }

    /*增*/
    @PostMapping("insert/{sname}/{age}/{cid}")
    @ApiOperation(value = "新增学生信息")
    public int insert(
        @ApiParam(value = "学生姓名", name = "sname", required = true)
        @PathVariable String  sname,
        @ApiParam(value = "学生年龄", name = "age", required = true)
        @PathVariable Integer age,
        @ApiParam(value = "班级编号", name = "cid", required = true)
        @PathVariable Integer cid
    ){
        Student student = new Student(sname,age,cid);

        return studentMapper.insert(student);
    }

    /*改*/
    @PutMapping("update/{sid}")
    @ApiOperation(value = "根据id修改学生信息")
    public Result update(
            @ApiParam(value = "学生id", name = "sid", required = true)
            @PathVariable Integer sid){
        Student student = new Student("呃",58,2);
        student.setSid(sid);
        return studentService.updateById(student);
    }

    /*删*/
    @DeleteMapping("delete/{sid}")
    @ApiOperation(value = "根据删除学生信息")
    public int delete(
            @ApiParam(value = "学生id", name = "sid", required = true)
            @PathVariable Integer sid
    ){
        int i = studentMapper.deleteById(sid);
        return i;
    }
}
